﻿#include "widgettcpcfg.h"
#include "ui_widgettcpcfg.h"
#include "commconfig.h"

WidgetTcpCfg::WidgetTcpCfg(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WidgetTcpCfg)
{
    ui->setupUi(this);
}

WidgetTcpCfg::~WidgetTcpCfg()
{
    delete ui;
}

void WidgetTcpCfg::setComm(BaseCommunication *comm)
{
    TcpCfg cfg=CommConfig::instance().getTcpCfg(comm->name());
    ui->lineEdit->setText(cfg.address);
    ui->spinBox->setValue(cfg.timeout);
    _comm=comm;
}

void WidgetTcpCfg::on_btnOpen_clicked()
{
    QJsonObject obj={
        {"address",ui->lineEdit->text()},
        {"timeout",ui->spinBox->value()}
    };
    _comm->setConfig(obj);

    TcpCfg cfg;
    cfg.address=ui->lineEdit->text();
    cfg.timeout=ui->spinBox->value();
    CommConfig::instance().setTcpCfg(_comm->name(),cfg);

    if(_comm->connectToHost())
    {
        ui->btnOpen->setEnabled(false);
    }
}

void WidgetTcpCfg::on_btnClose_clicked()
{
    _comm->disConnect();
    ui->btnOpen->setEnabled(true);
}
